package com.zx._12_算法;

import cn.hutool.core.util.ArrayUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import lombok.Data;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StopWatch;

/**
 * 升序排序
 */
public class Arrs {

    private static List<AA> arrs;
    private static int arr_nums = 1000;
    private static int arr_max_len = 1000_0;

    private static void init() {
        if (!CollectionUtils.isEmpty(arrs)) {
            return;
        }

        Arrs.arrs = new ArrayList<>(arr_nums);

        System.out.println("初始化排序测试数据，begin");

        Random r = new Random();
        for (int i = 0; i < arr_nums; i++) {
            int[] arr = new int[i];
            for (int j = 0; j < arr.length; j++) {
                int num = r.nextInt(arr_max_len);
                if (num % 10 == 0) {
                    num = 0 - num;
                }
                arr[j] = num;

            }
            Arrs.arrs.add(new AA(arr));
        }
        System.out.println("初始化排序测试数据, end, 测试案例：" + arr_nums + " 个");
    }

    public static void execute(IAS... iass) {
        init();

        StopWatch sw = new StopWatch();
        for (IAS ias : iass) {
            sw.start(ias.getClass().getSimpleName());
            for (AA aa : Arrs.arrs) {
                int[] arr = aa.getArr();
                ias.sort(arr);
                aa.setArrAfter(arr);

                boolean res = ArrayUtil.equals(arr, aa.getSorted());
                aa.setRes(res);

                if (!res) {
                    aa.print(ias);
                    break;
                }
            }
            System.out.println("排序校验结束, ias=["
                    + ias.getClass().getSimpleName()
                    + "]");
            sw.stop();

        }

        System.out.println(sw.prettyPrint());
    }

    public static void swap(int[] nums, int a, int b) {
        int tmp = nums[a];
        nums[a] = nums[b];
        nums[b] = tmp;
    }

    public interface IAS {

        void sort(int[] nums);
    }

    public static class Sort0_基准对比 implements IAS {

        @Override
        public void sort(int[] nums) {
            Arrays.sort(nums);
        }
    }

    @Data
    public static class AA {

        //排序前-不能修改
        private int[] arrBefore;
        //排序后的数据，算法获得
        private int[] arrAfter;
        //排序后的数据，内置排序答案-不能修改
        private int[] sorted;
        // 排序结果
        private boolean res;

        public AA(int[] arrBefore) {
            this.arrBefore = arrBefore;

            this.sorted = getArr();
            Arrays.sort(this.sorted);
        }

        public int[] getArr() {
            return this.arrBefore.clone();
        }

        public void print(IAS ias) {
            System.out.println("######################################");
            System.out.println("排序前：" + Arrays.toString(this.arrBefore));
            System.out.println("排序后：" + Arrays.toString(this.arrAfter));

            if (!res) {
                System.out.println("排序结果不正确["
                        + ias.getClass().getSimpleName()
                        + "]预期结果："
                        + Arrays.toString(this.sorted));
            }

        }


    }
}
